<?php

namespace App\Jobs;

use App\Models\Posts\WxPost;
use App\Models\Posts\WxPostsTag;
use App\Models\User\WxUserVisit;
use App\Wen\Utils\PostUtils;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;

class UpdateUserVisitJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $user_id = 0;
    // 1:笔记 2:圈子 3:商品 4:用户 5:话题
    protected $type = 0;
    protected $object_id = null;
    protected $need_check = true;
    public $tries = 1;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($uid, $type, $object_id, $need_check)
    {
        //
        $this->user_id = $uid;
        $this->type = $type;
        $this->object_id = $object_id;
        $this->need_check = $need_check;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        if(_empty_($this->user_id) || _empty_($this->type) || _empty_($this->object_id)){
            return;
        }

        if($this->need_check){
//            if(!UserUtils::is_user_online($this->user_id)){
//                return;
//            }
        }

        if(Cache::has('visit:'.$this->user_id.':'.$this->type.':'.$this->object_id)){
            return;
        }
        if($this->type == 2){
            // 连续访问圈子
            $continuous = get_user_meta($this->user_id, 'continuous_to_circle:'.$this->object_id, 'j');
            if(_empty_($continuous)){
                $continuous = [
                    'days' => 0,
                    'updated_at' => ''
                ];
            }
        }
        $userVisit = WxUserVisit::where([
            ['user_id', '=', $this->user_id],
            ['type', '=', $this->type],
            ['object_id', '=', $this->object_id]
        ])->first();
        if($userVisit){
            if($this->type == 2){
                if($continuous['days'] > 0){
                    if(current_time('day') > $continuous['updated_at']){
                        $continuous['days'] += 1;
                        $continuous['updated_at'] = current_time('day');
                        update_user_meta($this->user_id, 'continuous_to_circle:'.$this->object_id, $continuous, 'j');
                    }
                }else{
                    $continuous['days'] += 1;
                    $continuous['updated_at'] = current_time('day');
                    update_user_meta($this->user_id, 'continuous_to_circle:'.$this->object_id, $continuous, 'j');
                }
                Cache::forget('user_circle_unread_count:'.$this->user_id);
            }
            $userVisit->increment('times');
        }else{
            $userVisit = new WxUserVisit();
            $userVisit->user_id = $this->user_id;
            $userVisit->type = $this->type;
            $userVisit->object_id = $this->object_id;
            $userVisit->times = 1;
            $userVisit->save();

            if($this->type == 2){
                // 圈子连续访问天数
                $continuous['days'] += 1;
                $continuous['updated_at'] = current_time('day');
                update_user_meta($this->user_id, 'continuous_to_circle:'.$this->object_id, $continuous, 'j');
            }
        }
        if($this->type == 1){
            Cache::forget('post:view:count:'.$this->object_id);
            PostUtils::get_view_count($this->object_id);
        }
        Cache::put('visit:'.$this->user_id.':'.$this->type.':'.$this->object_id, 1,300);

        if($this->type == 1){
            $post = WxPost::where('id', $this->object_id)->first();
            $circle_id = $post->circle_id;
            if($circle_id){
                UpdateUserVisitJob::dispatch($this->user_id, 2, $circle_id, false);
            }

            $post_user_id = $post->user_id;
            if($post_user_id){
                UpdateUserVisitJob::dispatch($this->user_id, 4, $post_user_id, false);
            }

            $tag_ids = WxPostsTag::where('posts_id', $this->object_id)->pluck('tags_id');
            if($tag_ids){
                foreach ($tag_ids as $tag_id){
                    UpdateUserVisitJob::dispatch($this->user_id, 5, $tag_id, false);
                }
            }
        }

    }
}
